<?php
include("../contents.php");
include("../security.php");
include("../settings.php");
include("../database.php");
echo "<"."?xml version=\"1.0\"?".">";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>Administration</title>
    <link media="screen" rel="stylesheet" href="css/style.css" type="text/css" />
    <script src="js/scripts.js" type="text/javascript"></script>
    </head>

    <body id="editgrey">
    <?php
    if(!empty($_GET["dir"]) || (isset($_GET["dir"]) && $_GET["dir"]==0)) {
    	if (!empty($_GET["deldirlink"]) && $_GET["deldirlink"]=="1") {
    		mysql_query("UPDATE ". $GLOBALS["db_pref"] ."navigation SET linktype='0', link='' WHERE id='". $_GET["dir"] ."'");
    	}
    	
    	if (!empty($_GET["deldirgraphic"]) && getright("navigation_delete")) {
    		$field="graphic";
    		if($_GET["deldirgraphic"]=="2") {
    			$field="overgraphic";
    		}
    		mysql_query("UPDATE ". $GLOBALS["db_pref"] ."navigation SET ". $field ."='' WHERE id='". $_GET["dir"] ."'");
    	}

    	if ($_GET["dir"]==0 && (isset($_GET["pres"]) && Contents::presence_rights($_GET["pres"]))) {
    		$row_act=new rowact();
    		$row_act->id=0;
    		$row_act->presence=$_GET["pres"];
    		$row_act->name="/";
    	} else {
    		$act_dir=mysql_query("SELECT * FROM ". $GLOBALS["db_pref"] ."navigation WHERE id='". $_GET["dir"] ."'");
    		if (mysql_num_rows($act_dir)==1) {
    			$row_act=mysql_fetch_object($act_dir);
    			if (!Contents::presence_rights($row_act->presence)) {
    				die("Sie haben keinen Zugriff auf dieses Verzeichnis");
    			}
    		} else {
    			die("Da ist irgendwas schief gelaufen.");
    		}
    	}

    	if (!empty($_GET["deldir"]) && getright("navigation_delete")) {
    		mysql_query("UPDATE ". $GLOBALS["db_pref"] ."navigation SET deleted='1' WHERE id='". $_GET["deldir"] ."' and subof='". $row_act->id ."'");
    	}

    	if (!empty($_GET["sortdown"])) {
    		$act_sortdir=mysql_query("SELECT id,`sort` FROM ". $GLOBALS["db_pref"] ."navigation WHERE id='". $_GET["sortdown"] ."' and subof='". $row_act->id ."' and presence='". $row_act->presence ."' and deleted='0'");
    		$row_act_sortdir=mysql_fetch_object($act_sortdir);
    		$next_sortdir=mysql_query("SELECT id,`sort` FROM ". $GLOBALS["db_pref"] ."navigation WHERE `sort`>'". $row_act_sortdir->sort ."' and subof='". $row_act->id ."' and presence='". $row_act->presence ."' and deleted='0' LIMIT 1");
    		$row_next_sortdir=mysql_fetch_object($next_sortdir);
			mysql_query("UPDATE ". $GLOBALS["db_pref"] ."navigation SET `sort`='". $row_next_sortdir->sort ."' WHERE id='". $row_act_sortdir->id ."'");
			mysql_query("UPDATE ". $GLOBALS["db_pref"] ."navigation SET `sort`='". $row_act_sortdir->sort ."' WHERE id='". $row_next_sortdir->id ."'");
    	}

    	if (!empty($_GET["sortup"])) {
    		$act_sortdir=mysql_query("SELECT id,`sort` FROM ". $GLOBALS["db_pref"] ."navigation WHERE id='". $_GET["sortup"] ."' and subof='". $row_act->id ."' and presence='". $row_act->presence ."' and deleted='0'");
    		$row_act_sortdir=mysql_fetch_object($act_sortdir);
    		$next_sortdir=mysql_query("SELECT id,`sort` FROM ". $GLOBALS["db_pref"] ."navigation WHERE `sort`<'". $row_act_sortdir->sort ."' and subof='". $row_act->id ."' and presence='". $row_act->presence ."' and deleted='0' ORDER BY `sort` DESC LIMIT 1");
    		$row_next_sortdir=mysql_fetch_object($next_sortdir);
			mysql_query("UPDATE ". $GLOBALS["db_pref"] ."navigation SET `sort`='". $row_next_sortdir->sort ."' WHERE id='". $row_act_sortdir->id ."'");
			mysql_query("UPDATE ". $GLOBALS["db_pref"] ."navigation SET `sort`='". $row_act_sortdir->sort ."' WHERE id='". $row_next_sortdir->id ."'");
    	}

    	if (!empty($_GET["rename_dir_id"]) && !empty($_GET["rename_dir"]) && getright("navigation_rename")) {
    		// print_r($_SERVER);
    		$_GET["rename_dir"]=utf8_decode(urldecode($_GET["rename_dir"]));
    		$string=explode("rename_dir=", utf8_decode($_SERVER["REQUEST_URI"]));
    		$_GET["rename_dir"]=utf8_decode(urldecode($string[1]));
    		echo $_GET["rename_dir"];
    		$shortname=Contents::create_unique_name($_GET["rename_dir"], "navigation", $row_act->presence);
    		mysql_query("UPDATE ". $GLOBALS["db_pref"] ."navigation SET name='". $_GET["rename_dir"] ."', shortname='". $shortname ."' WHERE id='". $_GET["rename_dir_id"] ."' and subof='". $row_act->id ."' and presence='". $row_act->presence ."'");
    	}

    	if (!empty($_GET["create_dir"]) && getright("navigation_add")) {
    		$_GET["create_dir"]=utf8_decode(urldecode($_GET["create_dir"]));
    		$max=mysql_query("SELECT max(`sort`) as max FROM ". $GLOBALS["db_pref"] ."navigation WHERE subof='". $row_act->id ."' and deleted='0' and presence='". $row_act->presence ."'");
    		$dir_exists=mysql_query("SELECT id FROM ". $GLOBALS["db_pref"] ."navigation WHERE name='". $_GET["create_dir"] ."' and subof='". $row_act->id ."' and presence='". $row_act->presence ."' and deleted='0'");
    		if (mysql_num_rows($dir_exists)==0) {
    			$row_max=mysql_fetch_object($max);
    			$shortname=Contents::create_unique_name($_GET["create_dir"], "navigation", $row_act->presence);
    			mysql_query("INSERT INTO ". $GLOBALS["db_pref"] ."navigation SET name='". $_GET["create_dir"] ."', subof='". $row_act->id ."', presence='". $row_act->presence ."', deleted='0', shortname='". $shortname ."', sort='". ($row_max->max + 1) ."'");
    		}
    	}
    	?>
    	<div id="overview">
    	  <h1>Inhalt des Verzeichnisses &quot;<?php echo $row_act->name; ?>&quot;</h1>
    	  <img src="images/dir_symbol.gif" width="64" height="64" alt="" style="padding-right:20px;float:left;" />
    	  <p>Hier finden Sie alle Navigationspunkte.</p>
    	  <br style="clear:both;" />
	    </div>
	    <div id="edit">
	    <?php
    	if ($row_act->id!=0) {
    		?>
    		<p id="pageheadline">Verlinkung</p>
    		<table style="width:100%;padding:0px; margin:0px;border-top:0px;border-left:1px solid #aaaaaa;border-right:1px solid #aaaaaa;border-bottom:1px solid #aaaaaa; background-color:#ffffff" cellspacing="0" cellpadding="0">
    			<tr>
    				<th style="border:1px solid #666666;">Name</th>
    				<?php if (getright("navigation_rename")) { ?>
    					<th style="padding-left:5px;padding-right:5px;border-top:1px solid #666666;border-right:1px solid #666666;border-bottom:1px solid #666666;" width="50">Verlinkung</th>
    				<?php } ?>
    				<?php if (getright("navigation_delete")) { ?>
    					<th style="padding-left:5px;padding-right:5px;border-top:1px solid #666666;border-right:1px solid #666666;border-bottom:1px solid #666666;" width="50">Aktion</th>
    				<?php } ?>
    			</tr>
    			<tr onmouseover="setbgcolor(this, '#eeeeee')" onmouseout="setbgcolor(this, '#ffffff')">
    				<td class="tdnormal">
    					<?php
    					if ($row_act->linktype!=0 && $row_act->link!='') {
    						if ($row_act->linktype==1) {
    							$linkdoc=mysql_query("SELECT * FROM ". $GLOBALS["db_pref"] ."content_documents WHERE id='". $row_act->link ."' and checkin_by!='0' and deleted='0'");
    							if (mysql_num_rows($linkdoc)==0) {
    								echo "<em>Keine Verlinkung</em>";
    							} else {
    								$row_linkdoc=mysql_fetch_object($linkdoc);
    								echo "<a href=\"admin_content_edit.php?name=". $row_linkdoc->id2 ."\">". $row_linkdoc->pagetitle ."</a>";
    							}
    						} elseif ($row_act->linktype==2) {
    							echo "<a href=\"". $row_act->link ."\" target=\"_blank\">Externer Link: ". $row_act->link ."</a>";
    						} elseif ($row_act->linktype==3) {
    							$linknav=mysql_query("SELECT * FROM ". $GLOBALS["db_pref"] ."navigation WHERE id='". $row_act->link ."' and presence='". $row_act->presence ."' and deleted='0'");
    							if (mysql_num_rows($linknav)==0) {
    								echo "<em>Keine Verlinkung</em>";
    							} else {
    								$row_linknav=mysql_fetch_object($linknav);
    								echo "<a href=\"admin_content_edit.php?dirname=". $row_linknav->id ."\">Navigation: ". $row_linknav->name ."</a>";
    							}
    							// echo "<a href=\"". $row_act->link ."\" target=\"_blank\">". $row_act->link ."</a>";
    						}
    					} else {
    						echo "<em>Keine Verlinkung</em>";
    					}
    					?>
    				</td>
    				<td class="tdcenter">
    				  <a href="admin_navigation_add_link.php?dir=<?php echo $row_act->id; ?>"><img src="images/search.gif" style="border:0px; width: 14px; height: 14px;" alt="Suchen" /></a>
    				</td>
    				<?php if (getright("navigation_delete")) { ?>
    					<td class="tdcenter"><a href="javascript:askdelete('<?php echo $_SERVER["PHP_SELF"]; ?>?dir=<?php echo $row_act->id; ?>&deldirlink=1')"><img src="images/delete.gif" alt="L&ouml;schen" title="L&ouml;schen" style="border:0px;width:14px;height:16px;" /></a></td>
    				<?php } ?>
    			</tr>
    		</table>
    		<p id="pageheadline">Navigationsgrafiken</p>
    		<table style="width:100%;padding:0px; margin:0px;border-top:0px;border-left:1px solid #aaaaaa;border-right:1px solid #aaaaaa;border-bottom:1px solid #aaaaaa; background-color:#ffffff" cellspacing="0" cellpadding="0">
    			<tr>
    				<th style="border:1px solid #666666;" width="150" align="left">Bild</th>
    				<th style="padding-left:5px;padding-right:5px;border-top:1px solid #666666;border-right:1px solid #666666;border-bottom:1px solid #666666;" align="left">Dateiname</th>
   					<th style="padding-left:5px;padding-right:5px;border-top:1px solid #666666;border-right:1px solid #666666;border-bottom:1px solid #666666;" width="50" align="center">Verlinkung</th>
   					<th style="padding-left:5px;padding-right:5px;border-top:1px solid #666666;border-right:1px solid #666666;border-bottom:1px solid #666666;" width="50" align="center">Aktion</th>
    			</tr>
    			<tr onmouseover="setbgcolor(this, '#eeeeee')" onmouseout="setbgcolor(this, '#ffffff')">
    				<td class="tdnormal">Navigations-Bild:</td>
    				<td class="tdnormal">
    					<?php
    					if ($row_act->graphic!="") {
  							$graphic=mysql_query("SELECT ". $GLOBALS["db_pref"] ."media_dirs.name as dirname,". $GLOBALS["db_pref"] ."media.orig_filename FROM ". $GLOBALS["db_pref"] ."media INNER JOIN ". $GLOBALS["db_pref"] ."media_dirs ON ". $GLOBALS["db_pref"] ."media.ofdir = ". $GLOBALS["db_pref"] ."media_dirs.id WHERE ". $GLOBALS["db_pref"] ."media.id='". $row_act->graphic ."'");
  							if (mysql_num_rows($graphic)==1) {
  								$row_graphics=mysql_fetch_object($graphic);
  								echo $row_graphics->dirname ."/". $row_graphics->orig_filename;
  							}
  							
    					} else {
    						echo "<em>Kein Bild</em>";
    					}
    					?>
    				</td>
    				<td class="tdcenter">
    				  <a href="admin_navigation_add_graphic.php?dir=<?php echo $row_act->id; ?>&amp;mouseover=0"><img src="images/search.gif" alt="Suchen" title="Suchen" style="border:0px; width: 14px; height: 14px;" /></a>
    				</td>
   					<td class="tdcenter"><a href="javascript:askdelete('<?php echo $_SERVER["PHP_SELF"]; ?>?dir=<?php echo $row_act->id; ?>&deldirgraphic=1')"><img src="images/delete.gif" alt="L&ouml;schen" title="L&ouml;schen" style="border:0px;width:14px;height:16px;" /></a></td>
    			</tr>
    			<tr onmouseover="setbgcolor(this, '#eeeeee')" onmouseout="setbgcolor(this, '#ffffff')">
    				<td class="tdnormal">Navigations-MouseOver-Bild:</td>
    				<td class="tdnormal">
    					<?php
    					if ($row_act->overgraphic!="") {
  							$overgraphic=mysql_query("SELECT ". $GLOBALS["db_pref"] ."media_dirs.name as dirname,". $GLOBALS["db_pref"] ."media.orig_filename FROM ". $GLOBALS["db_pref"] ."media INNER JOIN ". $GLOBALS["db_pref"] ."media_dirs ON ". $GLOBALS["db_pref"] ."media.ofdir = ". $GLOBALS["db_pref"] ."media_dirs.id WHERE ". $GLOBALS["db_pref"] ."media.id='". $row_act->overgraphic ."'");
  							if (mysql_num_rows($overgraphic)==1) {
  								$row_overgraphics=mysql_fetch_object($overgraphic);
  								echo $row_overgraphics->dirname ."/". $row_overgraphics->orig_filename;
  							}
  							
    					} else {
    						echo "<em>Kein Bild</em>";
    					}
    					?>
    				</td>
    				<td class="tdcenter">
    				  <a href="admin_navigation_add_graphic.php?dir=<?php echo $row_act->id; ?>&amp;mouseover=1"><img src="images/search.gif" alt="Suchen" style="border:0px; width: 14px; height: 14px;" /></a>
    				</td>
   					<td class="tdcenter"><a href="javascript:askdelete('<?php echo $_SERVER["PHP_SELF"]; ?>?dir=<?php echo $row_act->id; ?>&deldirgraphic=2')"><img src="images/delete.gif" style="border:0px;width:14px;height:16px;" /></a></td>
    			</tr>
    		</table>
    		<?php
    	}
    	$subdirs=mysql_query("SELECT * FROM ". $GLOBALS["db_pref"] ."navigation WHERE subof='". $row_act->id ."' and presence='". $row_act->presence ."' and deleted='0' order by `sort`");
    	if (mysql_num_rows($subdirs)>0) {
    		$maxsubdirs=mysql_query("SELECT max(`sort`) as maxsort, min(`sort`) as minsort FROM ". $GLOBALS["db_pref"] ."navigation WHERE subof='". $row_act->id ."' and presence='". $row_act->presence ."' and deleted='0' order by `sort`");
    		$row_maxsubdirs=mysql_fetch_object($maxsubdirs);
    		?>
    		<div id="pageheadline">&nbsp;</div>
    		<table style="width:100%;padding:0px; margin:0px;border-top:0px;border-left:1px solid #aaaaaa;border-right:1px solid #aaaaaa;border-bottom:1px solid #aaaaaa; background-color:#ffffff" cellspacing="0" cellpadding="0">
    		<tr>
    			<th style="border:1px solid #666666;" align="left">Verzeichnisname</th>
    			<th style="padding-left:5px;padding-right:5px;border-top:1px solid #666666;border-right:1px solid #666666;border-bottom:1px solid #666666;" width="30" align="left">&Uuml;bersetzung</th>
    			<th style="padding-left:5px;padding-right:5px;border-top:1px solid #666666;border-right:1px solid #666666;border-bottom:1px solid #666666;" width="80" align="left">Aktion</th>
    		</tr>
    		<?php
    		for($i=0; $i<mysql_num_rows($subdirs); $i++) {
    			$row_subdirs=mysql_fetch_object($subdirs);
    			?>
    			<tr onmouseover="setbgcolor(this, '#eeeeee')" onmouseout="setbgcolor(this, '#ffffff')">
    				<td class="tdnormal"><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?dir=<?php echo $row_subdirs->id; ?>"><?php echo utf8_encode($row_subdirs->name); ?></a></td>
    				<td class="tdcenter"><a href="admin_navigation_lang.php?dir=<?php echo $row_subdirs->id; ?>&pres=<?php echo $row_act->presence; ?>"><img src="images/edit.gif" style="border:0px;width:12px;height:16px;" /></a></td> 				
    				<td class="tdnormal">
    					<?php if (getright("navigation_rename")) { ?>
    					<a href="javascript:askrenamedir('<?php echo $_SERVER["PHP_SELF"]; ?>?dir=<?php echo $row_act->id; ?>&rename_dir_id=<?php echo $row_subdirs->id; ?>&pres=<?php echo $row_act->presence; ?>','<?php echo $row_subdirs->name; ?>')"><img src="images/rename.gif" title="Umbenennen" alt="Umbenennen" style="border:0px;width:16px;height:16px;" /></a>
	    				<?php } ?>
    					<?php if (getright("navigation_delete")) { ?>
    					<a href="javascript:askdelete('<?php echo $_SERVER["PHP_SELF"]; ?>?dir=<?php echo $row_act->id; ?>&deldir=<?php echo $row_subdirs->id; ?>&pres=<?php echo $row_act->presence; ?>')"><img src="images/delete.gif" title="L&ouml;schen" alt="L&ouml;schen" style="border:0px;width:14px;height:16px;" /></a>
    					<?php } ?>
    					<?php
    					if($row_subdirs->sort>$row_maxsubdirs->minsort) {
    						?><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?dir=<?php echo $row_act->id; ?>&sortup=<?php echo $row_subdirs->id; ?>&pres=<?php echo $row_act->presence; ?>"><img src="images/sort_up.gif" width="16" height="16" alt="Nach oben" title="Nach oben" border="0" /></a><?php
    					} else {
    						?><img src="images/s.gif" width="16" height="16" alt="" title="" border="0" /><?php
    					}
    					if($row_subdirs->sort<$row_maxsubdirs->maxsort) {
    						?><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?dir=<?php echo $row_act->id; ?>&sortdown=<?php echo $row_subdirs->id; ?>&pres=<?php echo $row_act->presence; ?>"><img src="images/sort_down.gif" width="16" height="16" alt="Nach unten" title="Nach unten" border="0" /></a><?php
    					} else {
    						?><img src="images/s.gif" width="16" height="16" alt="" title="" border="0" /><?php
    					}
    					?>
    				</td>
    			</tr>
    			<?php
    		}
    		?>
    		</table>
    		<?php
    	}
    	if (getright("navigation_add")) { ?>
    	<a href="javascript:asknewdir('<?php echo $_SERVER["PHP_SELF"]; ?>?&dir=<?php echo $_GET["dir"]; echo ($row_act->id==0)?"&pres=". $row_act->presence:""; ?>')" class="small">Neuen Men&uuml;punkt hinzuf&uuml;gen</a><?php
    	}
    	?>
    	</div>
    	<?php
    }
    ?>
    </body>
    </html>